package com.czxy.controller;


import com.czxy.domain.Manager;
import com.czxy.service.ManagerService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private ManagerService managerService;


    //登录
    @RequestMapping("/login")
    public  String login(Manager manager , Model model , HttpSession session){


        Manager loginM = managerService.login(manager);

        if (loginM == null){
            //登录失败
            //存储失败信息

            model.addAttribute("loginMsg","用户名或密码不正确");
            //存储错的登录名和密码
            model.addAttribute("manager",manager);

            //请求转发
            return "/index.jsp";

        }else {

            //登录成功
            //往session存数据
            session.setAttribute("loginM",loginM);

            //重定向
            return "redirect:/pages/home.jsp";




        }


    }


    @RequestMapping("/logOut")
    public  String logOut(HttpSession session){

        session.invalidate();
        return "redirect:/index.jsp";
    }


    @RequestMapping("/findByInfo")
    public  String findByInfo(@RequestParam(defaultValue = "1")  Integer pageNum , @RequestParam(defaultValue = "5") Integer pageSize , Manager manager , Model model){


        PageInfo pageInfo = managerService.findByInfo(pageNum, pageSize, manager);

        model.addAttribute("pageInfo",pageInfo);


        return "/pages/list.jsp";
    }


















}
